<#import "../common/layout.ftl" as layout/>
<#import "../common/dataTables.ftl" as dataTables/>
<#import "../common/modern-forms.ftl" as mform/>
<#import "../common/modal.ftl" as modal/>

<!DOCTYPE html>
<html>
<@layout.head title="二维码管理">
    <@dataTables.head/>
    <@dataTables.buttons_head/>
    <@dataTables.select_head/>
    <@dataTables.fixedHeader_head/>
    <@mform.head/>
    <@select.head/>
</@layout.head>
<body>
<@layout.page>
    <div class="main-box">
        <div class="content">
            <!-- 表格 -->
            <div style="position: relative; top:0px; ">
                <@dataTables.commonTable id="mainTable" />
            </div>
            <!-- 按钮 -->
            <div id="btnGroup" class="btn-group" style="position: absolute; top:0px;">
                <button type="button" class="btn btn-blue btn-sm"
                        onclick="javascript:refreshData();">
                    <i class="fa fa-refresh"></i> 刷新
                </button>
                <button id="btnAddTopMenu" type="button" class="btn btn-blue btn-sm" style="margin-left: 5px;"
                        onclick="javascript:newdetail();">
                    <i class="fa fa-plus"></i> 新增
                </button>
                <button id="btnAddTopMenu" type="button" class="btn btn-blue btn-sm" style="margin-left: 5px;"
                        onclick="javascript:deldetail();">
                    <i class="fa fa-minus"></i> 删除
                </button>
                <button id="btnAddTopMenu" type="button" class="btn btn-blue btn-sm" style="margin-left: 5px;"
                        onclick="javascript:exportQRcode();">
                    <i class="fa fa-qrcode"></i> 导出二维码图片
                </button>

            </div>
        </div>
    </div>

    <!-- 弹出窗 -->

    <@modal.editModal id="modal-detail" buttonId="btndetailSave"  buttonName="保存" width="300px" height="100px">
        <@mform.container id="editForm" >
            <input id="id" name="id" type="hidden"/>
            <@mform.input_text id='num' name='num' size=12 label='数量' placeholder='数量' icon='fa-edit' />
        </@mform.container>
    </@modal.editModal>
</@layout.page>
<@layout.foot>
    <@dataTables.foot/>
    <@dataTables.buttons_foot/>
    <@dataTables.select_foot/>
    <@dataTables.fixedHeader_foot/>
    <@mform.foot/>
    <script src="${request.contextPath}/assets/js/jquery.serializejson.min.js?v=3653060703"></script>
    <script type="text/javascript">

        var state = {
            <#list state as item>
            "${item.code}": "${item.name}",
            </#list>
        }
        var columns = [
            {'sWidth': '200px', 'title': 'id', 'data': 'id'},
            {'sWidth': '100px', 'title': '二维码', 'data': 'code'},
            {'sWidth': '100px', 'title': '创建时间', 'data': 'createTime'},
            {'sWidth': '100px', 'title': '激活时间', 'data': 'activeTime'},
            {'sWidth': '100px', 'title': '状态', 'data': 'state'}
        ]

        var mainTable = $('#mainTable').winningTable({
            columns: columns,
            pageLength:20,
//        toolbar: "#btnGroup",
            fixedHeader: false,
            searching: true,
            ordering: false,
            "columnDefs": [{
                "targets": [0],
                "visible": false
            }],
            buttons: [

            ],
            "rowCallback": function (row, data, index) {
                $('td', row).eq(3).html(state[data.state]);
            }
        });

        function newdetail() {
            $('#modal-detail').find(".modal-title").text('新增二维码');
            clearForm($('#modal-Ddetail'));
            $('#modal-detail').modal();
            $("#btndetailSave").unbind();
            $("#btndetailSave").bind("click", savedetail);
        }
        function editdetail() {
            var datas = mainTable.getSelectedDatas();
            if (datas == null || datas.length == 0 || datas.length > 1) {
                swal("错误", "请选择一行数据", "error");
                return;
            }
            var $modal = $("#modal-detail");
            $modal.find(".modal-title").text("修改资产信息");
            clearForm($modal);
            var data = datas[0];
            initFormItemData($("#editForm"), data);
            $("#btndetailSave").unbind();
            $("#btndetailSave").click(savedetail);
            $modal.modal();
        }
        function refreshData() {
            mainTable.queryData("${request.contextPath}/QRcode/information/queryList");
        }
        refreshData();

        function savedetail() {
            swal({
                title: "确定保存",
                text: "是否确定保存此数据",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "是的,确定",
                cancelButtonText: "取消"
            }).then(function (result) {
                if (!result.value)
                    return;
                var requestData = $("#modal-detail form").serializeJSON();
                var params = {datas: JSON.stringify(requestData)};
                $.ajax({
                    type: 'POST',
                    data: params,
                    url: '${request.contextPath}/QRcode/information/save',
                    success: function (data) {
                        //成功之后
                        if (data.success) {
                            swal({
                                title: "成功",
                                text: "操作成功!",
                                type: "success",
                                showCancelButton: false,
                                confirmButtonText: "确定"
                            }).then(function (result) {
                                $("#modal-detail").modal('hide');
                                refreshData();
                            });
                        } else {
                            swal("错误", data.errorMsg, "error");
                        }
                    },
                    error: function (xhr, type) {
                        swal("错误", "系统发生内部错误!请稍后再试!", "error");
                    }
                });
            });
        }
        function deldetail() {
            var datas = mainTable.getSelectedDatas();
            if (datas == null || datas.length == 0 || datas.length > 1) {
                swal("错误", "请选择一行数据", "error");
                return;
            }
            var data = datas[0];
            swal({
                title: "确定删除",
                text: "是否确定删除此数据",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "是的,确定",
                cancelButtonText: "取消"
            }).then(function (result) {
                if (!result.value)
                    return;
                var requestData = {
                    id: data.id
                };
                var params = {datas: JSON.stringify(requestData)};
                $.ajax({
                    type: 'POST',
                    data: params,
                    url: '${request.contextPath}/QRcode/information/delete',
                    success: function (data) {
                        //成功之后
                        if (data.success) {
                            swal({
                                title: "成功",
                                text: "操作成功!",
                                type: "success",
                                showCancelButton: false,
                                confirmButtonText: "确定"
                            }).then(function (result) {
                                refreshData();
                            });
                        } else {
                            swal("错误", data.errorMsg, "error");
                        }
                    },
                    error: function (xhr, type) {
                        swal("错误", "系统发生内部错误!请稍后再试!", "error");
                    }
                });
            });
        }
        function exportQRcode(){
            var datas = mainTable.getSelectedDatas();
            if (datas == null || datas.length == 0 ) {
                swal("错误", "请选择数据", "error");
                return;
            }
            var codes="";
            for(var i=0;i<datas.length;i++){
                codes+=(","+datas[i].code);
            }
            window.open("/QRcode/information/print/"+codes.substr(1));
        }
    </script>
</@layout.foot>
</body>
</html>